package com.medicine.hbp.pay.wx;


import org.json.JSONObject;
import org.kymjs.kjframe.KJHttp;
import org.kymjs.kjframe.http.HttpCallBack;

import com.medicine.hbp.R;
import com.tencent.mm.opensdk.constants.Build;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class PayActivity extends Activity {

	private IWXAPI api;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView( R.layout.pay);

		api = WXAPIFactory.createWXAPI(this, "wxb4ba3c02aa476ea1");

		Button appayBtn = (Button) findViewById(R.id.appay_btn);
		appayBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				String url = "http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php?plat=android";
				final Button payBtn = (Button) findViewById(R.id.appay_btn);
				payBtn.setEnabled(false);
				Toast.makeText(PayActivity.this, "获取订单中...", Toast.LENGTH_SHORT).show();
				KJHttp kjhttp=new KJHttp();
				kjhttp.get( url, new HttpCallBack() {
					@Override
					public void onSuccess(String t) {
						super.onSuccess( t );
						try{
							JSONObject json = new JSONObject(t);
							if(null != json && !json.has("retcode") ){
								PayReq req = new PayReq();
								//req.appId = "wxf8b4f85f3a794e77";  // 测试用appId
								req.appId			= json.getString("appid");
								req.partnerId		= json.getString("partnerid");
								req.prepayId		= json.getString("prepayid");
								req.nonceStr		= json.getString("noncestr");
								req.timeStamp		= json.getString("timestamp");
								req.packageValue	= json.getString("package");
								req.sign			= json.getString("sign");
								req.extData			= "app data"; // optional
								Toast.makeText(PayActivity.this, "正常调起支付", Toast.LENGTH_SHORT).show();
								// 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
								api.sendReq(req);
							}else{
								Log.d("PAY_GET", "返回错误"+json.getString("retmsg"));
								Toast.makeText(PayActivity.this, "返回错误"+json.getString("retmsg"), Toast.LENGTH_SHORT).show();
							}
						}catch(Exception e){
							Log.e("PAY_GET", "异常："+e.getMessage());
							Toast.makeText(PayActivity.this, "异常："+e.getMessage(), Toast.LENGTH_SHORT).show();
						}
						payBtn.setEnabled(true);
					}

					@Override
					public void onFailure(int errorNo, String strMsg) {
						super.onFailure( errorNo, strMsg );
					}
				} );
			}
		});
		Button checkPayBtn = (Button) findViewById(R.id.check_pay_btn);
		checkPayBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				boolean isPaySupported = api.getWXAppSupportAPI() >= Build.PAY_SUPPORTED_SDK_INT;
				Toast.makeText(PayActivity.this, String.valueOf(isPaySupported), Toast.LENGTH_SHORT).show();
			}
		});
	}

}
